<?php

App::uses('AppController', 'Controller');

/**
 * Productos Controller
 *
 * @property Producto $Producto
 * @property PaginatorComponent $Paginator
 */
class ProductosController extends AppController
{

    public $layout = 'inventario';
    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator','RequestHandler');
    public $uses = array('Producto');

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->Producto->recursive = 0;
        $this->set('productos', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        if (!$this->Producto->exists($id))
        {
            throw new NotFoundException(__('Invalid producto'));
        }
        $options = array('conditions' => array('Producto.' . $this->Producto->primaryKey => $id));
        $this->set('producto', $this->Producto->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post'))
        {
            $this->Producto->create();
            if ($this->Producto->save($this->request->data))
            {
                $this->Session->setFlash(__('The producto has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The producto could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->Producto->exists($id))
        {
            throw new NotFoundException(__('Invalid producto'));
        }
        if ($this->request->is(array('post', 'put')))
        {
            if ($this->Producto->save($this->request->data))
            {
                $this->Session->setFlash(__('The producto has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The producto could not be saved. Please, try again.'));
            }
        } else
        {
            $options = array('conditions' => array('Producto.' . $this->Producto->primaryKey => $id));
            $this->request->data = $this->Producto->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->Producto->id = $id;
        if (!$this->Producto->exists())
        {
            throw new NotFoundException(__('Invalid producto'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Producto->delete())
        {
            $this->Session->setFlash(__('The producto has been deleted.'));
        } else
        {
            $this->Session->setFlash(__('The producto could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }
    
    public function listado()
    {
        $productos = $this->Producto->find('all');
        $this->set(compact('productos'));
    }
    
    public function guarda()
    {
        $this->layout = 'ajax';
        $productoidd = $_POST['data']['Producto']['idd'];
        $this->request->data['Producto']['nombre'] = $_POST['data']['Producto']['nombre'];
        $this->request->data['Producto']['descripcion'] = $_POST['data']['Producto']['descripcion'];
        $this->request->data['Producto']['uporcaja'] = $_POST['data']['Producto']['cantidad'];
        //debug($productoidd);
        
        if(!empty($productoidd) && $productoidd != 'nada')
        {
            $this->Producto->id = $productoidd;
            $this->Producto->save($this->request->data);
        }
        else{
            
            $this->Producto->create();
            if($this->Producto->save($this->request->data)){
                $productoidd = $this->Producto->getLastInsertID();
            }
            
        }
        //debug($_POST['data']['Producto']['id']);exit;
        
        //debug($productoidd);exit;
        
        $array['productoid'] = $productoidd;
        //$mens = array("primero"=>'Hola', "segundo"=>'mundo');
        $this->respond($array, true);
        //$this->respond('holaaa');
        /*$this->Producto->create();
        $this->Producto->save($this->request->data);*/
    }
    public function elimina($idProducto = null)
    {
        $this->layout = 'ajax';
        //debug($idProducto);exit;
        if(!empty($idProducto))
        {
            $this->Producto->delete($idProducto);
        }
        $this->respond($array, true);
    }
    public function prueba()
    {
        
    } 
    function respond($message=null, $json=false) {
        if ($message!=null) {
            if ($json==true) {
                $this->RequestHandler->setContent('json', 'application/json');
                $message=json_encode($message);
            }
            $this->set('message', $message);
        }
        $this->render('message');
    } 
      

}
